{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Chapter 05\n",
    "# 将数据插入字符串\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "name = 'James'\n",
    "height = 181.18"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T15:55:31.721700Z",
     "end_time": "2024-07-02T15:55:31.721700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "James has a height of 181.18 cm.\n"
     ]
    }
   ],
   "source": [
    "# 使用 + 运算符\n",
    "str_1 = name + ' has a height of ' + str(height) + ' cm.'\n",
    "print(str_1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T15:55:38.398103Z",
     "end_time": "2024-07-02T15:55:38.398103Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "James has a height of 181.180 cm.\n"
     ]
    }
   ],
   "source": [
    "# 使用 %\n",
    "str_2 = '%s has a height of %.3f cm.' % (name, height)\n",
    "print(str_2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T15:55:47.587313Z",
     "end_time": "2024-07-02T15:55:47.588302Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "James has a height of 181.180 cm.\n"
     ]
    }
   ],
   "source": [
    "# 使用 str.format()\n",
    "str_3 = '{} has a height of {:.3f} cm.'.format(name, height)\n",
    "print(str_3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T15:56:00.507706Z",
     "end_time": "2024-07-02T15:56:00.507706Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "James has a height of 181.180 cm.\n"
     ]
    }
   ],
   "source": [
    "# 使用f-strings\n",
    "str_4 = f'{name} has a height of {height:.3f} cm.'\n",
    "print(str_4)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-02T15:56:16.859783Z",
     "end_time": "2024-07-02T15:56:16.859783Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
